package com.xyht.sca_s.student_manage_system.modules.attendance.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyht.sca_s.student_manage_system.modules.attendance.entity.DataNode;
import com.xyht.sca_s.student_manage_system.modules.attendance.entity.SmsAttendance;
import com.xyht.sca_s.student_manage_system.modules.attendance.entity.SmsAttendanceDetail;
import com.xyht.sca_s.student_manage_system.modules.attendance.mapper.SmsAttendanceDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.attendance.mapper.SmsAttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.attendance.constant.AttendanceStatusConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.attendance.constant.AttendanceTaskStatusConstant.ATTENDANCE_TASK_STATUS_CLOSE;
import static com.xyht.sca_s.student_manage_system.modules.attendance.constant.AttendanceTaskStatusConstant.ATTENDANCE_TASK_STATUS_NOT_FINISH;
import static com.xyht.sca_s.student_manage_system.modules.attendance.constant.CommonConstant.ATTENDANCE_MIN_STUDENT_NUM;

@Component
public class CheckOutlierUtil {
    @Autowired
    private SmsAttendanceDetailMapper smsAttendanceDetailMapper;
    @Autowired
    private SmsAttendanceMapper smsAttendanceMapper;

    public void checkOutlier(String attendance_id) {
        //已签到学生
        List<SmsAttendanceDetail> positionList = smsAttendanceDetailMapper.selectList(new QueryWrapper<SmsAttendanceDetail>()
                .lambda()
                .eq(SmsAttendanceDetail::getAttendanceId,attendance_id)
                .isNotNull(SmsAttendanceDetail::getLongitude));
        if (positionList == null || positionList.size() < ATTENDANCE_MIN_STUDENT_NUM) {
            //修改未签到学生状态
            SmsAttendanceDetail updateAttendanceDetail = new SmsAttendanceDetail();
            updateAttendanceDetail.setAttendanceStatus(ATTENDANCE_STATUS_NOT_SIGN_IN);
            smsAttendanceDetailMapper.update(updateAttendanceDetail, new QueryWrapper<SmsAttendanceDetail>()
                    .lambda()
                    .eq(SmsAttendanceDetail::getAttendanceId,attendance_id)
                    .eq(SmsAttendanceDetail::getAttendanceStatus, ATTENDANCE_STATUS_DOING));

            SmsAttendance smsAttendance = new SmsAttendance();
            smsAttendance.setId(attendance_id);
            smsAttendance.setStatus(ATTENDANCE_TASK_STATUS_NOT_FINISH);
            smsAttendanceMapper.updateById(smsAttendance);
            return;
        }
        //所有学生
        List<SmsAttendanceDetail> studentAttendanceList = smsAttendanceDetailMapper.selectList(new QueryWrapper<SmsAttendanceDetail>()
                .lambda()
                .eq(SmsAttendanceDetail::getAttendanceId, attendance_id));
        ArrayList<DataNode> dataNodeList = new ArrayList<>();
        if (studentAttendanceList != null && studentAttendanceList.size() > 0) {
            for (SmsAttendanceDetail detail : studentAttendanceList) {
                if (isNullOrEmpty(detail.getLatitude()) || isNullOrEmpty(detail.getLongitude())) {
                    continue;
                }
                double[] a = {Double.parseDouble(detail.getLongitude()), Double.parseDouble(detail.getLatitude())};
                dataNodeList.add(new DataNode(detail.getId(), a));
            }
            OutlierNodeDetectUtil lof = new OutlierNodeDetectUtil();
            List<DataNode> nodeList = lof.getOutlierNode(dataNodeList);

            //修改已签到学生状态
            for (DataNode datanode : nodeList) {
                SmsAttendanceDetail smsAttendanceDetail = new SmsAttendanceDetail();
                int status;
                if (datanode.getLof() > 1) {
                    status = ATTENDANCE_STATUS_POSITION_ERR;
                } else {
                    status = ATTENDANCE_STATUS_NORMAL;
                }
                smsAttendanceDetail.setAttendanceStatus(status);
                smsAttendanceDetailMapper.update(smsAttendanceDetail, new QueryWrapper<SmsAttendanceDetail>()
                        .lambda()
                        .eq(SmsAttendanceDetail::getId, datanode.getNodeName())
                        .ne(SmsAttendanceDetail::getAttendanceStatus, ATTENDANCE_STATUS_TAB_NORMAL));
            }
            //修改未签到学生状态
            SmsAttendanceDetail updateAttendanceDetail = new SmsAttendanceDetail();
            updateAttendanceDetail.setAttendanceStatus(ATTENDANCE_STATUS_NOT_SIGN_IN);
            smsAttendanceDetailMapper.update(updateAttendanceDetail, new QueryWrapper<SmsAttendanceDetail>()
                    .lambda()
                    .eq(SmsAttendanceDetail::getAttendanceStatus, ATTENDANCE_STATUS_DOING));

            SmsAttendance smsAttendance = new SmsAttendance();
            smsAttendance.setId(attendance_id);
            smsAttendance.setStatus(ATTENDANCE_TASK_STATUS_CLOSE);
            smsAttendanceMapper.updateById(smsAttendance);
        }
    }
}
